package com.example.model;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Data;

/**
 * rest服务接口统一返回值对象 
 *
 * @author changwei
 * @date 2018年9月21日
 * @param <T> 返回的data数据类型
 */
@Data
@JsonIgnoreProperties(ignoreUnknown=true)
public class ActionResult<T> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2298957468403819613L;

	/**
	 * 请求结果状态
	 */
	private Boolean success;
	
	/**
	 * 请求响应码
	 */
	private String code;
	
	/**
	 * 请求响应信息
	 */
	private String msg;
	
	/**
	 * 请求响应数据
	 */
	private T data;
	
	/**
	 * 构造响应结果为成功的构造器
	 * @return
	 */
	public static <T> ActionResultBuilder<T> success(){
		ActionResultBuilder<T> actionResultBuilder = new ActionResultBuilder<T>();
		actionResultBuilder.withStatus(ActionResultStatus.SUCCESS);
		return actionResultBuilder;
	}
	
	/**
	 * 构造响应结果为服务器内部异常的构造器
	 * @return
	 */
	public static <T> ActionResultBuilder<T> serverError(){
		ActionResultBuilder<T> actionResultBuilder = new ActionResultBuilder<T>();
		actionResultBuilder.withStatus(ActionResultStatus.INTERNAL_SERVER_ERROR);
		return actionResultBuilder;
	}
	
	/**
	 * 构造响应结果为服务器业务异常的构造器
	 * @return
	 */
	public static <T> ActionResultBuilder<T> businessError(){
		ActionResultBuilder<T> actionResultBuilder = new ActionResultBuilder<T>();
		actionResultBuilder.withStatus(ActionResultStatus.BUSINESS_ERROR);
		return actionResultBuilder;
	}
	
	public static class ActionResultBuilder<T>{
		
		private ActionResult<T> actionResult;
		
		public ActionResultBuilder() {
			actionResult = new ActionResult<T>();
		}
		
		public void withStatus(ActionResultStatus status) {
			actionResult.setCode(String.valueOf(status.getCode()));
			if(actionResult.getMsg() == null || actionResult.getMsg().length() == 0) {
				actionResult.setMsg(status.getReason());
			}
			actionResult.setSuccess(ActionResultStatus.SUCCESS.getCode() == status.getCode());
		}
		
		public ActionResultBuilder<T> withData(T data) {
			actionResult.setData(data);
			return this;
		}
		
		public ActionResultBuilder<T> withCode(String code) {
			actionResult.setCode(code);
			return this;
		}
		
		public ActionResultBuilder<T> withMsg(String msg) {
			actionResult.setMsg(msg);
			return this;
		}
		
		public ActionResultBuilder<T> withSuccess(boolean success) {
			actionResult.setSuccess(success);
			return this;
		}
		
		public ActionResult<T> build(){
			return actionResult;
		}
		
	}
	
	
	public enum ActionResultStatus {
		
		/**
		 * 成功.
		 */
		SUCCESS(200, "SUCCESS"),

		/**
		 * 服务器内部错误.
		 */
		INTERNAL_SERVER_ERROR(500, "INTERNAL_SERVER_ERROR"),
		
		/**
		 * 业务异常
		 */
		BUSINESS_ERROR(501, "BUSINESS_ERROR");
		
		/**
		 * 状态代码.
		 */
		private final int code;

		/**
		 * 原因描述.
		 */
		private final String reason;

		/**
		 * 构造ActionResultStatus.
		 *
		 * @param statusCode the status code
		 * @param reasonPhrase the reason phrase
		 */
		ActionResultStatus(final int statusCode, final String reasonPhrase) {
			this.code = statusCode;
			this.reason = reasonPhrase;
		}
		
		public int getCode() {
			return this.code;
		}
		
		public String getReason() {
			return this.reason;
		}
		
	}
}
